/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giant.services.pos;

import com.giant.entities.InInventory;
import com.giant.entities.pos.PInvoice;
import com.giant.entities.pos.PInvoiceProd;
import com.giant.entities.pos.PInvoiceSeller;
import com.giant.exceptions.OperacionInvalidaException;
import com.giant.services.IServicioPersistenciaLocal;
import com.giant.services.ServicioPersistencia;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Harold
 */
@Stateless
public class ServiceInvoice implements ServiceInvoiceLocal {

    //-----------------------------------------------------------
    // Atributos
    //-----------------------------------------------------------
    /**
     * Interface con referencia al servicio de persistencia en el sistema
     */
    @EJB
    private IServicioPersistenciaLocal persistencia;

    //-----------------------------------------------------------
    // Constructor
    //-----------------------------------------------------------
    /**
     * Constructor class
     */
    public ServiceInvoice() {
        persistencia = new ServicioPersistencia();
    }

    //-----------------------------------------------------------
    // Métodos
    //-----------------------------------------------------------
    /**
     *
     * @param invoice
     * @throws OperacionInvalidaException
     */
    @Override
    public void addInvoice(PInvoice invoice, List<PInvoiceProd> products, PInvoiceSeller invoiceSeller) throws OperacionInvalidaException {
        try {
            //TODO volver operacion transaccional

            //save invoice 
            persistencia.create(invoice);

            //save invoice products
            PInvoiceProd invoprodtmp;
            InInventory inventorytmp;
            for (int i = 0; i < products.size(); i++) {
                invoprodtmp = products.get(i);
                persistencia.create(invoprodtmp);

                //take out inventory
                inventorytmp = invoprodtmp.getIdInventory();
                inventorytmp.setStatus(false);
                //clear in process, quit block inventory
                inventorytmp.setInProcess(false);
                inventorytmp.setModDate(new java.util.Date());
                persistencia.update(inventorytmp);
            }
            //save invoice seller
            persistencia.create(invoiceSeller);

            //save tax invoice
            String queryTaxInvoice = "INSERT INTO p_invoice_tax(id_invoice, id_type_tax, total_value)select id_invoice, id_type_tax, IFNULL(sum(tax_value),0) as total_value from p_invoice_prod where id_invoice=(select max(id_invoice) from p_invoice) group by id_invoice, id_type_tax";
            persistencia.executeGenericSQLAdd(queryTaxInvoice);

        } catch (OperacionInvalidaException ex) {
            throw new OperacionInvalidaException(ex.getMessage());
        }
    }

    /**
     *
     * @param id
     * @throws OperacionInvalidaException
     */
    @Override
    public void removeInvoice(long id) throws OperacionInvalidaException {
        PInvoice v = (PInvoice) persistencia.findById(PInvoice.class, id);
        try {
            persistencia.delete(v);
        } catch (OperacionInvalidaException ex) {
            throw new OperacionInvalidaException(ex.getMessage());
        }
    }

    /**
     *
     * @return
     */
    @Override
    public List<PInvoice> getInvoices() {
        return persistencia.findAll(PInvoice.class);
    }

    /**
     *
     * @param a
     * @param map
     * @return
     */
    @Override
    public List<PInvoice> getInvoicesFilter(String a, Map<String, String> map) {
        return persistencia.executeSql(a, map);
    }

    @Override
    public PInvoice getLastInvoice() {
        String invoice = "select * from p_invoice order by id_invoice desc limit 1";

        return (PInvoice) persistencia.executeGenericSQLSerialized(invoice, PInvoice.class).get(0);
    }

    @Override
    public void updateInvoice(PInvoice invoice) {
        persistencia.update(invoice);
    }

    @Override
    public void annulInvoice(PInvoice invoice) {
        persistencia.update(invoice);
        String query="update in_inventory set status=1 where id_inventory in(select id_inventory from p_invoice_prod where id_invoice="+ invoice.getIdInvoice()+")";
        persistencia.executeGenericSQLAdd(query);
    }

    @Override
    public List<PInvoice> getInvoicesFilterSql(String sql) {
        
        return persistencia.executeObjectSql(sql);
    }
}
